﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
////申请空间
//Nood * AddNood(SLDatetype x) {
//	Nood* newnood = (Nood*)malloc(sizeof(Nood));//每次申请完空间要判断！
//	if (newnood == NULL) {
//		perror("malloc fail");
//		exit(1);
//	}
//	newnood->next = NULL;
//	newnood->date = x;
//	return newnood;
//}
////链表打印
//void SLTPrint(Nood* phead) {
//	Nood* pcur=phead;
//	while (pcur) {
//		printf("%d->", pcur->date);
//		pcur = pcur->next;
//	}
//	printf("NULL");
//	printf("\n");
//}
////链表尾插
//void SLTPushBack(Nood**pphead, SLDatetype x) {
//	assert(pphead);
//	//节点在最前面就得创建
//	Nood* newnood = AddNood(x);
// //头节点为NULL和非NULL两种情况
//	if (*pphead == NULL) {
//	   
//		*pphead = newnood;/*如果头节点==0，将开辟好并存放x的空间与头节点相连*/
// }
//	else {
//		//找尾
//		Nood* ptail = *pphead;
//		while (ptail->next) {
//			ptail = ptail->next;
//		}
//		//链接
//		ptail->next = newnood;
//	}
//    
//}
////链表头插
//void SLTPushFront(Nood** pphead, SLDatetype x) {
//	assert(pphead);
//	Nood* newnood = AddNood(x);
//	newnood->next = *pphead;//把旧节点的地址给next指针
//	*pphead = newnood;//将头更新给newnood
//
//}
////链表尾删
//void SLTPopBack(Nood** pphead) {
//
//	assert(pphead && *pphead);//若写成（**pphead）则表示即头节点指针指向的节点不为空，而不是断言指针本身不为空
//	//找尾+找倒数第二个
//	Nood* ptail = *pphead;
//	Nood* ptr = *pphead;
//	//一个链表和多个链表两种情况
//	if (ptail->next == NULL) {//1.ptail得解引用变一级指针
//		//2.->优先级高于*
//		free(ptail);
//		*pphead = NULL;//不能写为ptail = NULL;
//	}
//	else {
//		while (ptail->next) {//快慢指针 位置不能颠倒！
//			ptr = ptail;
//			ptail = ptail->next;
//		}
//		//释放+定空
//		free(ptail);
//		ptail = NULL;
//		ptr->next = NULL;
//	}
//}
////链表头删（先用指针变量把第二个节点存起来，释放后再用变量值赋给头节点）
//void SLTPopFront(Nood** pphead) {
//	//节点非空
//	assert(pphead && *pphead);
//	//
//	Nood * next=(*pphead)->next;
//	free(*pphead);//free（voide*）
//	*pphead = next;
//}
////链表查找
//Nood* SLTFind(Nood* phead, SLDatetype x){
//	assert(phead);
//	Nood* pcur = phead;
//	while (pcur) {
//		if (pcur->date == x) {
//			return pcur;
//			break;
//		}
//		else
//			pcur = pcur->next;
//		
//	}
//	return NULL;
//}
////链表指定位置前插入
//void SLTInsert(Nood** pphead, Nood* pos, SLDatetype x) {
//	assert(pphead && *pphead);
//	assert(pos);
//	Nood* newnood = AddNood(x);
//	if (pos == *pphead) {
//		SLTPushFront( pphead , x);
//	}
//	else {
//		Nood* prev = *pphead;
//		while (prev->next != pos) {
//			prev = prev->next;
//		}
//		prev->next = newnood;
//		newnood->next = pos;
//	}
//
//}
////链表指定位置后插入
//void SLTInsertAfter(Nood* pos, SLDatetype x) {
//	assert(pos);
//	Nood* newnood = AddNood(x);
//	newnood->next = pos->next;
//	pos->next = newnood;
//}
// //删除pos之前节点
//void SLTEraseFront(Nood** pphead, Nood* pos) {
//	assert(pphead && *pphead);
//	assert(pos);
//	if (pos == *pphead) {
//		SLTPopFront(pphead);
//	}
//	else {
//		Nood* patial = *pphead;
//		while (patial->next != pos) {
//			patial = patial->next;
//		}
//		patial->next = pos->next;
//		free(pos);
//		pos = NULL;
//	}
//}
////指定位置后删除
//	void SLTEraseAfter(Nood * pos) {
//		assert(pos && pos->next);
//		Nood* del = pos->next;
//		pos->next = del->next;
//		free(del);
//		del = NULL;
//	}
////销毁链表
//	void SListDesTroy(Nood**pphead) {
//		assert(pphead && *pphead);
//		Nood*pcur = *pphead;
//		while (pcur) {
//			Nood* next = pcur->next;
//			free(pcur);
//			pcur = next;			
//		}
//		*pphead = NULL;
//	}
// 节点创建
Nood* BuyNood(SLTdatetype x) {
	Nood* newnood = (Nood*)malloc(sizeof(Nood));//还要考虑创建失败的情况！！！
	if (newnood == NULL) {
		assert("malloc faill ! ");
		exit(1);
	}
	/*节点创建好后需对其中的date 与 next 操作！！！*/
	newnood->date = x;
	newnood->next = NULL;
	return newnood;
}
//节点打印
void PrintNood(Nood* phead) {
	Nood* pcur = phead;
	while (pcur) {
		printf("%d->", pcur->date);
		pcur = pcur->next;
		
	}
	printf("NULL");
	printf("\n");
}
//链表尾插
void SLTPushBack(Nood** pphead, SLTdatetype x) {
	assert(pphead);
	Nood* newnood = BuyNood(x);
	if (*pphead == NULL) {
		*pphead = newnood;
	}
	else {
		Nood* pcur = *pphead;
		while (pcur->next!=NULL) {
			pcur = pcur->next;
		}
		pcur->next = newnood;
	}
}
//链表头插
void SLTPushTop(Nood** pphead, SLTdatetype x) {
	assert(pphead);
	Nood* newnood = BuyNood(x);
	if (*pphead == NULL) {
		*pphead = newnood;
	}
	else {
		newnood->next = *pphead;
		*pphead = newnood;
	}
}
//链表尾删
void SLTPopBack(Nood** pphead)
{
	assert(pphead);
	Nood* pcur = *pphead;
	if (pcur->next == NULL) {
		free(pcur);
		pcur = NULL;
		*pphead = NULL;
	}
	else {
		Nood* next = NULL;
		while (pcur->next != NULL) {
			next = pcur;
			pcur = pcur->next;
		}		
		next->next = NULL;
		free(pcur);
	}
}
//链表头删
void SLTPopFront(Nood** pphead) {
	assert(pphead);
	Nood* pcur = *pphead;
	if (pcur->next == NULL) {
		free(*pphead);
		*pphead = NULL;
	}
	else {
		*pphead = pcur->next;
		free(pcur);
		pcur = NULL;
	}
}
//查找 
Nood* SLTFind(Nood* phead, SLTdatetype x) {
	assert(phead);
	Nood* pcur = NULL;
	pcur = phead;
	while (pcur->date!=x) {
		pcur = pcur->next;
		if (pcur == NULL)
			break;
	}
	return  pcur;
}
//在指定位置之前插⼊数据 
void SLTInsert(Nood** pphead, Nood* pos, SLTdatetype x) {
	assert(pphead && *pphead);
	Nood* newnood = BuyNood(x);
	if (pos == *pphead) {
		Nood* pcur = *pphead;//创建临时变量保存头
		*pphead = newnood;//移头
		newnood->next = pcur;//将新头的next指针指向临时变量

	}
	else {
		Nood* prev = *pphead;
		while (prev->next != pos) {
			prev = prev->next;
		}
		prev->next = newnood;
		newnood->next = pos;
	}
}
//删除pos节点 
void SLTErase(Nood** pphead, Nood* pos) {
	assert(pphead && *pphead);
	assert(pos);//确保传过来的数在链表里有
	if (pos == *pphead) {
		SLTPopFront(pphead);
	}
	else {
		Nood* prev = *pphead;
		Nood* next = pos->next;
		while (prev->next != pos) {
			prev = prev->next;
		}
        prev->next= next;
		free(pos);
		pos = NULL;
		
	}
}
//在指定位置之后插⼊数据 
void SLTInsertAfter(Nood* pos, SLTdatetype x) {
	assert(pos);
	Nood* newnood = BuyNood(x);
	Nood* next = pos->next;
	pos->next = newnood;
	newnood->next = next;
}
//删除pos之后的节点 (不会！！！)(精髓：存 连 删！！！）
void SLTEraseAfter(Nood* pos) {
	assert(pos&&pos->next);
	Nood* del = pos->next;//先用del把要删的的pos->next存储起来
	pos->next = pos->next->next;
	free(del);
	del= NULL;

}
//销毁链表 (精髓：存 连 删！！！）
void SListDesTroy(Nood** pphead) {
	Nood* pcur = *pphead;
	while (pcur) {
		Nood* del = pcur;
		pcur = pcur->next;
		free(del);
		del = NULL;

	}
	*pphead = NULL;//注意：出循环时pcur已经为NULL 为确保头指针不为野指针 则需将其设为NULL
}











